# 导入相关模块
from rest_framework import status
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import RetrieveModelMixin
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet, ReadOnlyModelViewSet
from booktest.models import BookInfo
from booktest.serializers import BookInfoSerializer
from rest_framework import mixins
from rest_framework.permissions import AllowAny,IsAuthenticated,IsAdminUser,IsAuthenticatedOrReadOnly, BasePermission


# 设置查询所有    基类APIView
# class BookListView(APIView):
#     def get(self,request):
#         books = BookInfo.objects.all()
#         serializer = BookInfoSerializer(books,many=True)
#         return Response(serializer.data)


# 设置实现获取所有图书信息的接口  GenericAPIView
# class BookListView(GenericAPIView):
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器
#     serializer_class = BookInfoSerializer
#i
#     def get(self, request):
#         # 查询数据库
#         qs = self.get_queryset()#指明使用的数据查询集，返回视图使用的查询集，视图指定的是全部的内容
#         # 实现序列化
#         seralizer = self.get_serializer(qs, many=True)
#         # 响应序列化结果
#         return Response(seralizer.data)

# 设置获取单一图书信息的视图
# class BookDetailView(GenericAPIView):
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器
#     serializer_class = BookInfoSerializer
#
#     def get(self,request,pk):
#         """
#         GET /books/<pk>/
#         :param request: Request类型的对象
#         :param pk: 要访问的数据库的信息，哪一个内容
#         :return: 返回JSON格式的数据
#         """
#         # 查询数据库
#         book = self.get_object()#在视图中调用此方法获取详情信息的模型类对象
#         # 实现序列化
#         serializer = self.get_serializer(book)
#         # 响应序列化结果
#         return Response(serializer.data)


# 扩展类
# ListModelMixin
# from rest_framework.mixins import ListModelMixin
#
# class BookListView(ListModelMixin,GenericAPIView):
#     # 指定查询集查询的内容
#     queryset = BookInfo.objects.all()
#     #指定序列化器
#     serializer_class = BookInfoSerializer
#
#     def get(self,request):
#         return self.list(request)


# RetrieveModelMixin  返回一个详情页
# class BookDetailview(RetrieveModelMixin,GenericAPIView):
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器
#     serializer_class = BookInfoSerializer
#
#     def get(self, request, pk):
#         return self.retrieve(request)


# 视图集  ViewSet
# class BookInfoViewSet(viewsets.ViewSet):
#     def list(self,request):
#         books = BookInfo.objects.all()
#         serializer = BookInfoSerializer(books,many=True)
#         return Response(serializer.data)
#     def retrieve(self,request,pk=None):
#         try:
#             books = BookInfo.objects.get(id=pk)
#         except BookInfo.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#         serializer = BookInfoSerializer(books)
#         return Response(serializer.data)


# 视图集子类 GenericViewSet
# class BookInfoViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer

# 视图集子类  ModelViewSet
# class BookInfoViewSet(ModelViewSet):
#     queryset = BookInfo.objects.all()
#     serializer_class = BookInfoSerializer

# 视图集子类  ReadOnlyModelViewSet
# class BookInfoViewSet(ReadOnlyModelViewSet):
#     # 指定查询集
#     queryset = BookInfo.objects.all()
#     # 指定序列化器
#     serializer_class = BookInfoSerializer


class MyPermission(BasePermission):
    def has_object_permission(self, request, view, obj):
        """控制对obj对象的访问权限，此案例决绝所有对对象的访问"""
        return False


# 视图集的中定义的自定义动作
class BookInfoViewSet(mixins.ListModelMixin,mixins.RetrieveModelMixin,GenericViewSet):
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer


    # 访问权限设置
    permission_classes = [IsAuthenticated,MyPermission]
    # @action(methods=['get'],detail=False)
    def latest(self,request):
        # 获取用户输入的id
        book = BookInfo.objects.latest('id')
        # 生成序列化器
        serializer = self.get_serializer(book)
        #返回序列化器的结果
        return Response(serializer.data)
    # @action(methods=['put'],detail=True)
    def read(self, request, pk):
        """修改图书阅读量"""
        book = self.get_object()
        #查找的内容必须与原来的一致  bread=bread
        book.bread = request.data.get('bread')
        book.save()
        serializer = self.get_serializer(book)
        return Response(serializer.data)




